<template>
  <div>
    <el-table
      :data="menulist"
      style="width: 100%"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="菜单编号" width="90"> </el-table-column>
      <el-table-column prop="title" label="菜单名称" width="160">
      </el-table-column>
      <el-table-column prop="pid" label="上级菜单" width="120">
      </el-table-column>

      <el-table-column label="菜单图标" width="120">
        <template slot-scope="item">
          <div>
            <!-- {{ item.row.icon }} -->
            <i :class="item.row.icon"></i>
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="url" label="菜单地址" width="180">
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            <el-tag effect="dark" type="success" v-if="item.row.status == 1">
              启用
            </el-tag>
            <el-tag effect="dark" type="warning" v-else> 禁用 </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="item">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="edit(item.row.id)"
            circle
          ></el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-delete"
            @click="del(item.row.id)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// 删除接口
import { menudelete } from "../../require/api";
//调vuex
import { mapGetters, mapActions } from "vuex";

export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({ menulist: "menu/getMenu" }),
  },
  methods: {
    ...mapActions({ getMenuAction: "menu/getMenuAction" }),
    // 编辑
    edit(id) {
      // console.log(id);
      this.$emit("edit", id);
    },
    // 删除一条数据
    del(id) {
      // console.log(id);
      // 确认删除提示框再次确认
      this.$confirm("确认删除该条信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          menudelete({ id }).then((res) => {
            console.log(res);
            if (res.code == 200) {
              // 更新页面
              this.getMenuAction();
              // 提示成功信息
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },

  mounted() {
    //页面一加载调用接口
    this.getMenuAction();
  },
};
</script>
<style lang=less scoped >
.el-table {
  min-height: 400px;
}
</style>
